﻿using System;
using System.Collections;
using System.Web;
using System.Web.Caching;

namespace visinia.SmartEngine.Cache
{
    /// <summary>
    /// The DataCache is used through out SmartEngine for data cache
    /// it can also be used in visinia Plugins for the data cache.
    /// </summary>
    public class DataCache
    {
        /// <summary>
        /// Returns the cached object
        /// </summary>
        /// <param name="key">Name of the cache key whose data to return</param>
        public static object GetCache(string key)
        {
            return HttpContext.Current.Cache[key];
        }

        /// <summary>
        /// Adds a new value in the cache
        /// </summary>
        /// <param name="key">The name of the key</param>
        /// <param name="value">The object to store</param>
        public static void SetCache(string key, object value)
        {
            if (value == null)
                return;
            HttpContext.Current.Cache.Insert(key, value);
        }

        /// <summary>
        /// Adds a new value in the cache
        /// </summary>
        /// <param name="key">The name of the key</param>
        /// <param name="value">The object to store</param>
        /// <param name="slidingExpiration">The sliding expiration time</param>
        public static void SetCache(string key, object value, TimeSpan slidingExpiration)
        {
            if (value == null)
                return;
            HttpContext.Current.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, slidingExpiration);
        }

        /// <summary>
        /// Adds a new cache value
        /// </summary>
        /// <param name="key">The name of the key</param>
        /// <param name="value">The object to store</param>
        /// <param name="dependencies">The dependencies</param>
        /// <param name="absoluteExpiration">Absolute expiration time</param>
        /// <param name="slidingExpiration">Sliding expiration time</param>
        public static void SetCache(string key, string value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            if (value == null)
                return;
            HttpContext.Current.Cache.Insert(key, value, dependencies, absoluteExpiration, slidingExpiration);
        }

        /// <summary>
        /// Deletes the specified key's object from the cache
        /// </summary>
        /// <param name="key">The key whose data to delete</param>
        public static void DeleteCache(string key)
        {
            HttpContext.Current.Cache.Remove(key);
        }

        /// <summary>
        /// Deletes multiple cached objects whose key name starts with the specified exp value
        /// </summary>
        /// <param name="exp">The value to check wether keys names start with this string</param>
        public static void DeleteCacheMultiple(string exp)
        {
            IDictionaryEnumerator CacheEnum = HttpContext.Current.Cache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                if (CacheEnum.Key.ToString().ToLower().StartsWith(exp.ToLower()))
                    DeleteCache(CacheEnum.Key.ToString());
            }
        }

        /// <summary>
        /// Deletes the whole cache
        /// </summary>        
        public static void ClearCache()
        {
            IDictionaryEnumerator CacheEnum = HttpContext.Current.Cache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                DeleteCache(CacheEnum.Key.ToString());
            }
        }
    }
}
